﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using GameSchool.Models;
using GameSchool.ViewModels.Event;
using GameSchool.DAL;

namespace GameSchool.Controllers
{
    public class EventController : Controller
    {
        private IEventRepository m_rep;

        public EventController()
        {
            m_rep = new EventRepository(new gsEntities());
        }

        public EventController(IEventRepository rep)
        {
            m_rep = rep;
        }

        [Authorize(Roles = "Admin")]
        public ActionResult Create()
        {
            var viewModel = new CreateViewModel();
            viewModel.event_ = new Event();

            return View(viewModel);
        }
       
        [HttpPost]
        [Authorize(Roles = "Admin")]
        public ActionResult Create(Event event_)
        {

            if (ModelState.IsValid)
            {
                m_rep.InsertEvent(event_);
                m_rep.Save();

                return RedirectToAction("List", new { eventCreated = event_  });
            }
            else
            {
                ModelState.AddModelError("", "Ekki eru allir reitir útfylltir. Vinsamlega farðu yfir innsláttinn og reyndu aftur.");
            }

            var viewModel = new CreateViewModel();
            viewModel.event_ = event_ ;
            

            return View(viewModel);

        }

        [Authorize(Roles = "Admin")]
        public ActionResult Edit(int id)
        {
            var viewModel = new EditViewModel();
            viewModel.event_ = m_rep.GetEventById(id);
            return View(viewModel);
        }

        [HttpPost]
        [Authorize(Roles = "Admin")]
        public ActionResult Edit(Event event_, int id)
    {
        var viewModel = new EditViewModel();

        if (ModelState.IsValid)
        {
            var eventToUpdate = m_rep.GetEventById(id);
            m_rep.UpdateEvent(eventToUpdate); 
            eventToUpdate.CompanyName = event_.CompanyName;
            eventToUpdate.Location = event_.Location;
            eventToUpdate.Description = event_.Description;
            eventToUpdate.EventStarts = event_.EventStarts;
            eventToUpdate.MaximumAttendees = event_.MaximumAttendees;
            eventToUpdate.RegistrationBegins = event_.RegistrationBegins;
            eventToUpdate.RegistrationEnds = event_.RegistrationEnds;

            m_rep.Save();

            return RedirectToAction("List", new {eventUpdated = event_.CompanyName });
        }

        else
        {
            ModelState.AddModelError("", "Ekki eru allir reitir útfylltir. Vinsamlega farðu yfir innsláttinn og reyndu aftur.");
        }

       
        return View(viewModel);
    }

        [Authorize(Roles = "Admin, Nemandi")]
        public ActionResult List(string updatedEvent, string createdEvent)
        {
            var viewModel = new ListViewModel();
            viewModel.eventListModel = m_rep.GetEvent();
            viewModel.eventJustUpdated = updatedEvent;
            viewModel.eventJustCreated = createdEvent;

            if (User.IsInRole("Admin"))
            {
                return View("ListAdmin", viewModel);
            }
            else if(User.IsInRole("Nemandi"))
            {

                return View("ListStudent", viewModel);
            }
            else
            {
                return RedirectToAction("LogOn", "Account");
            }
        }
    }
}
